package com.thx.tags;

import com.opensymphony.xwork2.util.ValueStack;
import com.thx.common.spring.BeanFactory;
import com.thx.common.web.SessionInfo;
import com.thx.common.web.WebConstant;
import com.thx.user.service.UserManager;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.components.Component;

import java.io.IOException;
import java.io.Writer;

import javax.servlet.http.HttpServletRequest;

/**
 * select标签的类.
 * 
 * @author shiyan
 */
public class User extends Component {

  private HttpServletRequest request;

  private String id;

  private String cssClass;

  private String value;

  private String href;

  private String onClick;

  private boolean more;


  public User(ValueStack stack, HttpServletRequest request) {
    super(stack);
    this.request = request;
  }

  @Override
  public boolean start(Writer writer) {
    if (StringUtils.isBlank(href)) {
      href = "javascript:void(0);";
    }
    String attrs = "";
    if (StringUtils.isNotBlank(id)) {
      attrs += "id=\"" + id + "\"";
    }
    if (StringUtils.isNotBlank(cssClass)) {
      if (more) {
        attrs += "class=\"thxcommon-userlink " + cssClass + "\"";
      } else {
        attrs += "class=\"" + cssClass + "\"";
      }

    } else {
      if (more) {
        attrs += "class=\"thxcommon-userlink \"";
      }
    }
    if (StringUtils.isNotBlank(onClick)) {
      attrs += "onclick=\"" + onClick + "\"";
    }
    String text = "";
    if (StringUtils.isBlank(value)) {
      SessionInfo info = (SessionInfo) request.getSession().getAttribute(WebConstant.KEY_USER);
      value = info.getUserId();
      text = info.getRealName();
    } else {
      UserManager userMgr = (UserManager) BeanFactory.getBean(UserManager.class);
      com.thx.user.model.User user = userMgr.get(value);
      if (user != null) {
        text = user.getRealName();
      }
    }
    attrs += "data-userId=\"" + value + "\"";
    
    String body = "<a href=\"%s\" %s >%s</a>";
    body = String.format(body, href, attrs, text);
    try {
      writer.write(body);
    } catch (IOException ex) {
      ex.printStackTrace();
    }
    return true;
  }

  public HttpServletRequest getRequest() {
    return request;
  }

  public void setRequest(HttpServletRequest request) {
    this.request = request;
  }

  public String getId() {
    return id;
  }

  public void setId(String id) {
    this.id = id;
  }

  public String getCssClass() {
    return cssClass;
  }

  public void setCssClass(String cssClass) {
    this.cssClass = cssClass;
  }

  public String getValue() {
    return value;
  }

  public void setValue(String value) {
    this.value = value;
  }

  public String getHref() {
    return href;
  }

  public void setHref(String href) {
    this.href = href;
  }

  public String getOnClick() {
    return onClick;
  }

  public void setOnClick(String onClick) {
    this.onClick = onClick;
  }

  public boolean getMore() {
    return more;
  }

  public void setMore(boolean more) {
    this.more = more;
  }

}
